package com.situ.car.controller;

import com.situ.car.pojo.entity.Car;
import com.situ.car.pojo.entity.CarBrand;
import com.situ.car.pojo.entity.Category;
import com.situ.car.pojo.query.CarQuery;
import com.situ.car.service.ICarBrandService;
import com.situ.car.service.ICarService;
import com.situ.car.service.ICategoryService;
import com.situ.car.service.Impl.CarBrandServiceImpl;
import com.situ.car.service.Impl.CarServiceImpl;
import com.situ.car.service.Impl.CategoryServiceImpl;
import com.situ.car.util.MyBeanUtils;
import com.situ.car.util.PageInfo;
import com.situ.car.util.Result;
import org.springframework.util.ObjectUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@WebServlet("/car")
public class CarController extends BaseController{
    private ICarService carService = new CarServiceImpl();
    private ICategoryService categoryService = new CategoryServiceImpl();
    private ICarBrandService carBrandService = new CarBrandServiceImpl();
    @Override
    protected void service(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        System.out.println("CarController.service");
        String method = req.getParameter("method");
        switch (method) {
            case "selectByPage" :
                selectByPage(req, resp);
                break;
            case "updateStatus" :
                updateStatus(req, resp);
                break;
            case "add" :
                add(req, resp);
                break;
            case "deleteById" :
                deleteById(req, resp);
                break;
            case "deleteAll" :
                deleteAll(req, resp);
                break;
            case "update" :
                update(req, resp);
                break;
            case "selectById" :
                selectById(req, resp);
                break;
            case "selectOrder":
                selectOrder(req, resp);
                break;
        }
    }

    private void selectOrder(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("CarController.selectOrder");
        List<Car> list = carService.selectOrder();
        toJSON(resp, Result.ok(list));
    }

    private void update(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("CarController.update");
        Car car = MyBeanUtils.copyToBean(req, Car.class);
        Integer carPD = carService.update(car);
        if (carPD != 0) {
            toJSON(resp, Result.ok("修改成功！"));
        } else {
            toJSON(resp, Result.error("此车牌号已经存在！"));
        }

    }

    private void selectById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("CarController.selectById");
        String id = req.getParameter("id");
        Car car = carService.selectById(Integer.parseInt(id));
        List<CarBrand> carBrandList = carBrandService.selectAll();
        List<Category> categoryList = categoryService.selectAll();
        Map<String, Object> map = new HashMap<>();
        map.put("car", car);
        map.put("carBrandList", carBrandList);
        map.put("categoryList", categoryList);
        toJSON(resp, Result.ok(map));
    }

    private void deleteAll(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("CarController.deleteAll");
        String[] ids = req.getParameterValues("ids[]");
        if(!ObjectUtils.isEmpty(ids)) {
            carService.deleteAll(ids);
            toJSON(resp, Result.ok("删除成功！"));
        } else {
            toJSON(resp, Result.error("没有数据要删除！"));
        }
    }

    private void deleteById(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("CarController.deleteById");
        String id = req.getParameter("id");
        carService.deleteById(Integer.parseInt(id));
        toJSON(resp, Result.ok("删除成功！"));
    }

    private void add(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("CarController.add");
        Car car = MyBeanUtils.copyToBean(req, Car.class);
        Integer licensePlateCount = carService.selectLicensePlateCount(car);
        if (licensePlateCount == 0) {
            carService.add(car);
            toJSON(resp, Result.ok("上架成功！"));
        } else {
            toJSON(resp, Result.error("此车牌号已经存在！"));
        }

    }

    private void updateStatus(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("CarController.updateStatus");
        String id = req.getParameter("id");
        String status = req.getParameter("status");
        carService.updateStatus(Integer.parseInt(id), Integer.parseInt(status));
        toJSON(resp, Result.ok("修改成功！"));
    }

    private void selectByPage(HttpServletRequest req, HttpServletResponse resp) {
        System.out.println("CarController.selectByPage");
        CarQuery carQuery = MyBeanUtils.copyToBean(req, CarQuery.class);
        PageInfo pageInfo = carService.selectByPage(carQuery);
        toJSON(resp, pageInfo);
    }
}
